package com.lijz.config;

import com.alibaba.fastjson.parser.ParserConfig;
import com.lijz.utils.FastJson2JsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author lijingzhao
 * @date 2018/10/9 19:39
 */
@Configuration
public class RedisConfig {

	@Bean
	public RedisSerializer fastJson2JsonRedisSerializer() {
		ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
		return new FastJson2JsonRedisSerializer<>(Object.class);
	}

	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory, RedisSerializer fastJson2JsonRedisSerializer) {
//		StringRedisTemplate redisTemplate = new StringRedisTemplate(factory);
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(factory);
		redisTemplate.setConnectionFactory(factory);
		//redis   开启事务
		redisTemplate.setEnableTransactionSupport(true);
		//hash  使用jdk  的序列化
		redisTemplate.setHashValueSerializer(fastJson2JsonRedisSerializer/*new JdkSerializationRedisSerializer()*/);
		//StringRedisSerializer  key  序列化
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		//keySerializer  对key的默认序列化器。默认值是StringSerializer
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//  valueSerializer
		redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}
}
